package com.woniuxy.finance.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.util.CheckTokenUtil;
import com.woniuxy.finance.model.dto.ConditionFindBusDto;
import com.woniuxy.finance.model.dto.ConditionsFindBusDto;
import com.woniuxy.finance.model.dto.UpdateBus;
import com.woniuxy.finance.model.dto.UpdateVIPByIdDto;
import com.woniuxy.finance.model.entity.BusinessCompetitor;
import com.woniuxy.finance.model.vo.ClientVo;
import com.woniuxy.finance.model.vo.FindClientNameAndBalanceVo;
import com.woniuxy.finance.service.BusinessCompetitorService;
import com.woniuxy.response.R;
import com.woniuxy.response.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 友商信息表; 前端控制器
 * </p>
 *
 * @author chenlin
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/businessCompetitor")
public class BusinessCompetitorController {
    @Resource
    private BusinessCompetitorService businessCompetitorService;

    /**
     * 查询所有的友商信息
     * @param type
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findClientAll/{type}/{page}/{size}")
    public ResponseResult<Page<BusinessCompetitor>> findClientAll(@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
    }
    /**
     * 按状态查询客户的信息
     * @param status 状态 ： 启用/禁用
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findClientByAcct/{acct}/{type}/{page}/{size}")
    public ResponseResult<Page<BusinessCompetitor>> findClientByStatus(@PathVariable("acct") String status,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        System.out.println(status);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientByAcct(status,type,page,size));
    }
    /**
     * 新增友商
     */
    @PostMapping("/addClient")
    @PreAuthorize("hasAuthority('com:addClient')")
    public ResponseResult<Boolean> addClient(@RequestBody BusinessCompetitor businessCompetitor, HttpServletRequest request){
        String u = request.getHeader("access_token");
        Map<String, Object> user = CheckTokenUtil.getUser(u);
//        Integer aid = (Integer) user.get("uid");
        String account = (String) user.get("account");
        boolean result = businessCompetitorService.addClient(businessCompetitor,account);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 多条件查询友商信息
     */
    @PostMapping("/conditionsFindBus")
    public ResponseResult<IPage<BusinessCompetitor>> conditionsFindBus(@RequestBody ConditionsFindBusDto condition){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.conditionsFindBus(condition));
    }
    /**
     * 修改友商信息
     */
    @PostMapping("/updateBusById")
    @PreAuthorize("hasAnyAuthority('com:updateClient','com:updateVendor')")
    public ResponseResult<Boolean> updateBusById(@RequestBody UpdateBus updateBus){
        boolean result = businessCompetitorService.updateBusById(updateBus);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 删除友商
     */
    @DeleteMapping ("/delBusById/{id}/{type}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:updateClient','com:updateVendor')")
    public ResponseResult<Page<BusinessCompetitor>> delBusById(@PathVariable("id") Integer id,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.delBusById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }

    /**
     * 关注
     */
    @PutMapping("/attBusById/{id}/{type}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:updateClient','com:updateVendor')")
    public ResponseResult<Page<BusinessCompetitor>> attClientById(@PathVariable("id") Integer id,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.attBusById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 取消关注
     */
    @PutMapping("/noAttBusById/{id}/{type}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:updateClient','com:updateVendor')")
    public ResponseResult<Page<BusinessCompetitor>> noAttBusById(@PathVariable("id") Integer id,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.noAttBusById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 取消关注 并返回所有关注的友商信息
     */
    @PutMapping("/noAttBusById2/{id}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:updateClient','com:updateVendor')")
    public ResponseResult<Page<BusinessCompetitor>> noAttBusById2(@PathVariable("id") Integer id, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.noAttBusById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findBus(page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 启用  com:openClient
     */
    @PutMapping("/openById/{id}/{type}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:openClient','com:openVendor')")
    public ResponseResult<Page<BusinessCompetitor>> openById(@PathVariable("id") Integer id,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.openById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 禁用
     */
    @PutMapping("/closeById/{id}/{type}/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('com:openClient','com:openVendor')")
    public ResponseResult<Page<BusinessCompetitor>> closeById(@PathVariable("id") Integer id,@PathVariable("type") Integer type, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        boolean result = businessCompetitorService.closeById(id);
        if(result) {
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientAll(type,page,size));
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",null);
        }
    }
    /**
     * 查询所有的供应商信息
     * @param page
     * @param size
     * @return
     */
//    @GetMapping("/findVendorAll/{page}/{size}")
//    public ResponseResult<Page<BusinessCompetitor>> findVendorAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
//        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findVendorAll(page,size));
//    }
    /**
     * 按状态查询供应商的信息
     * @param status 状态 ： 启用/禁用
     * @param page
     * @param size
     * @return
     */
//    @GetMapping("/findVendorByStatus/{status}/{page}/{size}")
//    public ResponseResult<Page<BusinessCompetitor>> findVendorByStatus(@PathVariable("status") String status,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
//        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findVendorByStatus(status,page,size));
//    }
    /**
     * 新增供应商
     */
//    @PostMapping("/addVendor")
//    @PreAuthorize("hasAuthority('com:addVendor')")
//    public ResponseResult<Boolean> addVendor(@RequestBody BusinessCompetitor businessCompetitor, HttpServletRequest request){
//        String u = request.getHeader("access_token");
//        Map<String, Object> user = CheckTokenUtil.getUser(u);
////        Integer aid = (Integer) user.get("uid");
//        String account = (String) user.get("account");
//        boolean result = businessCompetitorService.addVendor(businessCompetitor,account);
//        if(result) {
//            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
//        }else{
//            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
//        }
//    }
    /**
     * 查询关注的友商信息
     */
    @GetMapping("/findAttBus/{page}/{size}")
    public ResponseResult<Page<BusinessCompetitor>> findAttBus(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findBus(page,size));
    }

    /**
     * 查询关注的客户、供应商 信息
     */
    @GetMapping("/findBusByType/{type}/{page}/{size}")
    public ResponseResult<Page<BusinessCompetitor>> findBusByType(@PathVariable("type") Integer type,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findBusByType(type,page,size));
    }

    /**
     * 多条件查询关注的友商信息
     */
    @GetMapping("/conditionsFindAtt/{condition}/{page}/{size}")
    public ResponseResult<Page<BusinessCompetitor>> conditionsFindAtt(@PathVariable("condition") String condition,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.conditionsFindAtt(condition,page,size));
    }
    /**
     * 获取客户姓名 id
     */
    @GetMapping("/getName")
    public ResponseResult<List<ClientVo>> getName(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.getName());
    }
    @PostMapping("/findSupplierBycondition")
    public ResponseResult<List<BusinessCompetitor>> findSupplierBycondition(@RequestBody ConditionFindBusDto dto){

        return  new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findSupplierBycondition(dto.getName(),dto.getStatus(),dto.getType()));
    }
    /**
     * 获取客户名和余额
     *  按余额降序
     *  limit 6
     */
    @GetMapping("/findClientNameAndBalance")
    public ResponseResult<List<FindClientNameAndBalanceVo>> findClientNameAndBalance(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientNameAndBalance());
    }
    /**
     *获取客户名和充值总额
     * 按总额降序
     * limit 6
     */
    @GetMapping("/findClientNameAndRechargeTotal")
    public ResponseResult<List<FindClientNameAndBalanceVo>> findClientNameAndRechargeTotal(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.findClientNameAndRechargeTotal());
    }
    /**
     * 根据id修改客户VIP等级
     */
    @PutMapping("/updateVIPById")
    public ResponseResult<Boolean> updateVIPById(@RequestBody UpdateVIPByIdDto e){
        BusinessCompetitor businessCompetitor = new BusinessCompetitor();
        businessCompetitor.setId(e.getId());
        businessCompetitor.setVipLevel(e.getVipLevel());
        boolean b = businessCompetitorService.updateById(businessCompetitor);
        if(b){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }
        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
    }
    /**
     * 根据id获取邮箱
     */
    @GetMapping("/getEmailById/{id}")
    public ResponseResult<String> getEmailById(@PathVariable("id") Integer id){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",businessCompetitorService.getEmailById(id));
    }
}

